from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from .models import Activity, ActivityParticipant, ActivityTag
from .serializers import ActivitySerializer
from .serializers import ActivityParticipantSerializer
class ActivityView(APIView):
    permission_classes = []

    def get(self, request):
        """获取活动列表（支持多条件筛选）"""
        activities = Activity.objects.all()

        # 1. 我的活动 / 我参加的活动
        activity_type = request.GET.get('type')
        if activity_type == 'created':
            activities = activities.filter(creator_id=request.GET.get('user_id'))
        elif activity_type == 'joined':
            activities = activities.filter(participants__user_id=request.GET.get('user_id'))

        # 2. 状态筛选
        status = request.GET.get('status')
        if status:
            activities = activities.filter(status=status)

        # 3. 标签筛选（单标签）
        tag = request.GET.get('tag')
        if tag:
            activities = activities.filter(tags__name=tag)

        # 4. 日期范围
        start_date = request.GET.get('start_date')
        end_date   = request.GET.get('end_date')
        if start_date and end_date:
            activities = activities.filter(
                time__date__range=[start_date, end_date]
            )

        serializer = ActivitySerializer(
            activities.distinct(), many=True, context={'request': request}
        )
        return Response(serializer.data)

class ActivityParticipantView(APIView):
    permission_classes = []

    def get(self, request, activity_id):
        """获取活动参与者列表"""
        try:
            activity = Activity.objects.get(id=activity_id)
            participants = activity.participants.all()
            serializer = ActivityParticipantSerializer(participants, many=True)
            return Response(serializer.data)
        except Activity.DoesNotExist:
            return Response({'error': '活动不存在'}, status=status.HTTP_404_NOT_FOUND)

    def post(self, request, activity_id):
        """参加/取消参加活动"""
        try:
            activity = Activity.objects.get(id=activity_id)
            user_id = request.data.get('user_id')

            participant = ActivityParticipant.objects.filter(
                activity=activity,
                user_id=user_id
            ).first()

            if participant:
                participant.delete()
                return Response({'joined': False})
            else:
                ActivityParticipant.objects.create(
                    activity=activity,
                    user_id=user_id
                )
                return Response({'joined': True})
        except Activity.DoesNotExist:
            return Response({'error': '活动不存在'}, status=status.HTTP_404_NOT_FOUND)
class ActivityDeleteView(APIView):
    permission_classes = []

    def delete(self, request, activity_id):
        """删除活动"""
        try:
            activity = Activity.objects.get(id=activity_id)
            # 删除活动关联的所有标签
            activity.tags.all().delete()
            # 删除活动的所有参与者
            activity.participants.all().delete()
            # 最后删除活动本身
            activity.delete()
            return Response({'message': '活动已删除'}, status=status.HTTP_204_NO_CONTENT)
        except Activity.DoesNotExist:
            return Response({'error': '活动不存在'}, status=status.HTTP_404_NOT_FOUND)